﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HalalCheck.DataModels;
using Xamarin.Forms;
using XLabs.Forms.Controls;
using XLabs.Forms.Mvvm;

namespace HalalCheck.ViewModels
{
    public class HalalViewModel : BaseViewModel
    {
        private List<HalalItem> _halalList;
        public List<HalalItem> HalalList
        {
            get { return _halalList; }
            set
            {
                if (value != null)
                {
                    _halalList = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private List<HalalItem> _halalListfiltered;
        public List<HalalItem> HalalListFiltered
        {
            get { return _halalListfiltered; }
            set
            {
                if (value != null)
                {
                    _halalListfiltered = value;
                    NotifyPropertyChanged();
                }
            }
        }


        private string _search;
        public string Search
        {
            get { return _search; }
            set
            {
                _search = value;
                if (_search != null)
                {
                    HalalListFiltered = HalalList.Where(
                        deal => deal.Name.Trim().ToLower().Contains(_search.Trim().ToLower())
                                || deal.Id.Trim().ToLower().Contains(_search.Trim().ToLower())).ToList();
                }
            }
        }

        public HalalViewModel()
        {
            GetHalalListFromTxt();
            HalalListFiltered = HalalList;
        }

        private void GetHalalListFromTxt()
        {
            #region How to load a text file embedded resource
            var assembly = typeof(HalalViewModel).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream("HalalCheck.Resources.halaldb.txt");

            using (var reader = new StreamReader(stream))
            {
                var temp = new List<HalalItem>();

                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split('_');
                    HalalItem item = new HalalItem
                    {
                        Halal = Convert.ToInt32(values[1]),
                        Id = values[0],
                        Name = values[2],
                        Description = values[3],
                        HalalColor = GetHalalColor(Convert.ToInt32(values[1]))

                    };
                    temp.Add(item);
                }
                HalalList = temp;
            }
            #endregion
        }

        private Color GetHalalColor(int status)
        {
            switch (status)
            {
                case 0:
                    return Color.Lime;
                case 1:
                    return Color.Silver;
                case 2:
                    return Color.Red;
                default:
                    return Color.Lime;
            }

        }
    }
}
